package com.xposed.aliproxy.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.xposed.aliproxy.R;
import com.xposed.aliproxy.http.HttpBase;
import com.xposed.aliproxy.http.HttpManager;
import com.xposed.aliproxy.qrcode.GlobalInstance;
import com.xposed.aliproxy.utils.ActionUtil;
import com.xposed.aliproxy.utils.DateUtil;
import com.xposed.aliproxy.utils.ParamConfig;
import com.xposed.aliproxy.utils.Utils;

import org.json.JSONObject;
import de.robv.android.xposed.XposedBridge;

public class AliBalanceReceiver extends BroadcastReceiver {
    private Context context;
    private ParamConfig paramConfig;
    private String aliAccount;
    private String payformUrl;

    public AliBalanceReceiver(Context context, ParamConfig paramConfig) {
        this.context = context;
        this.paramConfig = paramConfig;
        this.aliAccount = ParamConfig.getInstance(context).getString("aliAccount", "");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(HttpManager.getSpliceUrl(context));
        stringBuilder.append("payform");
        this.payformUrl = stringBuilder.toString();
    }

    public synchronized void onReceive(Context context, Intent intent) {
        if (ActionUtil.RECEIVER_ALI_BALANCE.equals(intent.getAction())) {
            try {
                String stringExtra = intent.getStringExtra("balance");
                if (Double.parseDouble(stringExtra) != Double.parseDouble(GlobalInstance.getInstance().f1769h)) {
                    GlobalInstance.getInstance().f1769h = stringExtra;
                    JSONObject jSONObject = new JSONObject();
                    jSONObject.put("aid", "1");
                    jSONObject.put("transType", "G001");
                    jSONObject.put("title", "name");
                    jSONObject.put("tradeTime", DateUtil.formatData("yyyy-MM-dd HH:mm:ss"));
                    jSONObject.put("money", "0");
                    jSONObject.put("respMsg", "支付成功");
                    jSONObject.put("respCode", "00");
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.append(DateUtil.formatData("yyyyMMddHHmmssSSS"));
                    stringBuilder.append(Utils.randomStr(5));
                    jSONObject.put("orderNo", stringBuilder.toString());
                    jSONObject.put("inAccount", this.aliAccount);
                    jSONObject.put("balance", stringExtra);
                    jSONObject.put("qrCodeRemark", "remark");
                    jSONObject.put("isLogin", "1");
                    jSONObject.put("sign", Utils.formatJson(jSONObject, context.getString(R.string.app_xposed_jka)));
                    final String jSONObject2 = jSONObject.toString();
                    new Thread(new Runnable() {
                        public void run() {
                            try {
                                HttpBase.m2919a(AliBalanceReceiver.this.payformUrl, jSONObject2, null);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                } else {
                    return;
                }
            } catch (Throwable th) {
                XposedBridge.log(th);
            }
        } else {
            return;
        }
        return;
    }
}
